package com.youkeda.dewu.service.impl;

import com.youkeda.dewu.dao.PaymentRecordDAO;
import com.youkeda.dewu.dataobject.PaymentRecordDO;
import com.youkeda.dewu.model.PaymentRecord;
import com.youkeda.dewu.param.PaymentRecordQueryParam;
import com.youkeda.dewu.service.PaymentRecordService;
import com.youkeda.dewu.util.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName PaymentRecordServiceImpl.java
 * @Description TODO
 * @createTime 2020年10月13日 16:02:00
 */
@Service
public class PaymentRecordServiceImpl implements PaymentRecordService {
    @Autowired
    private PaymentRecordDAO paymentRecordDAO;

    @Override
    public PaymentRecord save(PaymentRecord paymentRecord) {
        if (paymentRecord == null){
            return null;
        }
        PaymentRecordDO paymentRecordDO = new PaymentRecordDO(paymentRecord);
        if (StringUtils.isEmpty(paymentRecord.getId())){
           paymentRecordDO.setId(UUIDUtils.getUUID());
           int insert = paymentRecordDAO.insert(paymentRecordDO);
            if (insert < 1){
                return null;
            }
        }
        int update = paymentRecordDAO.updateByPrimaryKey(paymentRecordDO);
        if (update <1){
            return null;
        }
        return paymentRecord;
    }

    @Override
    public List<PaymentRecord> query(PaymentRecordQueryParam queryParam) {
        if (queryParam == null){
            return null;
        }
        List<PaymentRecordDO> paymentRecordDOS = paymentRecordDAO.queryParam(queryParam);
        if (CollectionUtils.isEmpty(paymentRecordDOS) || paymentRecordDOS ==null){
            return null;
        }
        List<PaymentRecord> paymentRecords = paymentRecordDOS.stream().map(paymentRecordDO -> paymentRecordDO.toModel()).collect(Collectors.toList());

        return paymentRecords;
    }

    @Override
    public PaymentRecord updateStatus(PaymentRecord paymentRecord) {
        if (paymentRecord == null){
            return null;
        }
        PaymentRecordDO paymentRecordDO = new PaymentRecordDO(paymentRecord);
        int update = paymentRecordDAO.updateByPrimaryKeySelective(paymentRecordDO);
        if (update < 1){
            return null;
        }

        return paymentRecord;
    }
}
